package com.leaves.exception;

import com.leaves.response.ApiResult;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2021年06月21日 10时28分24秒
 * @Description: 404 处理
 */
@RestController
public class NotFoundHandle implements ErrorController {

    //@Override
    public String getErrorPath() {
        return "/error";
    }

    // 由于除404以外的异常都会被全局异常处理掉，所以走到这里的请求都是404了
    @RequestMapping("/error")
    public Object error(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 记录API访问日志
        ApiResult aj = ApiResult.definition(404, "Please enter the correct request address.");
        //SpApilogUtil.endRequest(aj);

        // 返回json消息
        response.setStatus(200);
        return aj;
    }
}
